/**
 * Created by Administrator on 2017/5/10.
 */

var Table = require('./table.js');
var Rules = require('./rules.js');
var Const = require('./const')

module.exports = {
    recom: function (cardsArray) {
        var filter1 = TableTodo(cardsArray);
        return Table2(filter1, cardsArray)
    }
}

function TableTodo(cardsArray) {
    var result = [];

    Table.table.forEach(function (v) {
        var objRe = {};
        var cards = [];
        v.forEach(function (v2) {
            cards.push(cardsArray[v2]);
        });

        var obj = {};
        cards.forEach(function (v) {
            obj[v] = 1;
        })
        var re = Rules.sumPattern(obj);
        if (re.pattern > Const.Pattern.PAIR) {
            objRe['head'] = {
                'pattern': re.pattern,
                'cards': cards
            }
            result.push(objRe);
        }
    });
    return result;
}

//var filter1 = TableTodo();

function Table2(filter1, cardsArray) {
    var result = [];

    var filteredCard = [];
    filter1.forEach(function (v) {
        result.push(todos(del5cards(cardsArray, v['head']['cards']), v));
    });

    var reduced = result.reduce(function (a, b) {
        return a.concat(b);
    }, []);
    var tuijian = [];
    var redFilter = reduced.filter(function (v) {
        if (Rules.isStandardComp(v)) {
            if (!contains(v, tuijian)) {
                return true;
            }
            //return true;
            return false;
        } else {
            return false;
        }
    });

    var filterdTuijian = tuijian.filter(v => {
        if (v['head']['pattern'] + v['body']['pattern'] + v['foot']['pattern'] >= 6) {
            return true;
        }
        return false;
    });


    // console.log(filterdTuijian[0])
    // console.log(filterdTuijian[1])
    // console.log(filterdTuijian[2])
    // console.log(filterdTuijian[3])
    // console.log(filterdTuijian[4])
    // console.log(filterdTuijian[5])
    // console.log(filterdTuijian[6])


    // console.log(tuijian.length);
    // console.log(filterdTuijian.length);
    // //console.log(redFilter[1])
    // console.log(redFilter.length);
    // console.log(reduced.length)
    // filterdTuijian.sort(function (a, b) {
    //     var scoreA = a['head']['pattern'] + a['body']['pattern'] + a['foot']['pattern'];
    //     var scoreB = b['head']['pattern'] + b['body']['pattern'] + b['foot']['pattern'];

    //     return scoreA < scoreB || filterdTuijian.indexOf(b) - filterdTuijian.indexOf(a);
    // });
    sort(filterdTuijian);


    return filterdTuijian.slice(0, 5);
}
function swap(a, b, arr) {
    var temp = clone(arr[a]);
    arr[a] = clone(arr[b]);
    arr[b] = temp;
}
function sort(obj) {
    var len = obj.length;
    for (var i = 0; i < len; ++i) {
        for (var j = i + 1; j < len - i; ++j) {
            var sum1 = obj[i]['head']['pattern'] + obj[i]['body']['pattern'] + obj[i]['foot']['pattern']
            var sum2 = obj[j]['head']['pattern'] + obj[j]['body']['pattern'] + obj[j]['foot']['pattern']
            if (sum1 < sum2) {
                swap(i, j, obj)
            }
        }
    }
}
//Table2();
function contains(des, res) {
    var len = res.length;
    if (len == 0) {
        res.push(des);
        return false;
    }

    for (var i = 0; i < len; ++i) {
        if (des['head']['pattern'] == res[i]['head']['pattern']
            && des['body']['pattern'] == res[i]['body']['pattern']
            && des['foot']['pattern'] == res[i]['foot']['pattern']) {
            //res.push(des)
            return true;
        }
    }
    res.push(des);
    return false;
}
function todos(cardsArray, objHead) {
    var result = [];

    Table.table2.forEach(function (v) {
        var objRe = {};
        var cards = [];
        v.forEach(function (v2) {
            cards.push(cardsArray[v2]);
        });

        var obj = {};
        cards.forEach(function (v) {
            obj[v] = 1;
        })
        var re = Rules.sumPattern(obj);
        if (re.pattern != Const.Pattern.NONE) {
            objRe['body'] = {
                'pattern': re.pattern,
                'cards': cards
            }
            objRe['foot'] = {
                'pattern': objHead['head'].pattern,
                'cards': objHead['head']['cards'].slice(),
            }
            objRe['head'] = {
                'pattern': Rules.checkHead(del5cards(cardsArray, cards)),
                'cards': del5cards(cardsArray, cards),
            }
            result.push(objRe);
        }
    });

    return result;
}
function TableTodo2(footer, cardsArray) {
    var result = [];
    Table.table2.forEach(function (v) {
        var objRe = {};
        var cards = [];
        v.forEach(function (v2) {
            cards.push(cardsArray[v2]);
        });
        var obj = {};
        cards.forEach(function (v) {
            obj[v] = 1;
        })
        var re = Rules.sumPattern(obj);
        if (re.pattern != Const.Pattern.NONE) {
            footer['body'] = {
                'pattern': re.pattern,
                'cards': cards
            }
            var headCards = del5cards(cardsArray, cards);
        }

    });
}
function del5cards(cardsArray, cardsDel) {
    return cardsArray.filter(function (v) {
        for (var i = 0; i < cardsDel.length; ++i) {
            if (v == cardsDel[i]) {
                return false;
            }
        }
        return true;
    });
}



